﻿using System;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Model;
using Kiiro.SiteDefinition.ControlTemplates;
using Kiiro.SiteDefinition.Core;

namespace Kiiro.SiteDefinition.ProjectPages
{
    public partial class Summary : BasePage
    {
        private Project project;

        public ActivityHistoryControl ActivityHistory
        {
            get { return ( ActivityHistoryControl ) v2ActivityHistoryControl; }
        }

        public ProjectSummary ProjectSummary
        {
            get { return ( ProjectSummary ) v2ProjectSummary; }
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
            if ( KiiroMaster != null
                && KiiroMaster.V2Header != null
                && KiiroMaster.V2Header.Project != null )
            {
                project = KiiroMaster.V2Header.Project;
            }
            else
            {
                project = ProjectsModel.GetByServerRelativeWebUrl ( SiteCollectionId, ServerRelativeUrl );
            }

            litTitle.Text = project != null && string.IsNullOrEmpty ( project.Title ) ? " - Summary" : "Project Summary";

            if ( project != null )
            {
                lblProjectTitle.Text = SPHelpers.ConvertHtmlToKiiroSafeText ( project.Title );
                lblDescription.Text = SPHelpers.ConvertHtmlToKiiroSafeText ( project.Description );

                if ( string.IsNullOrEmpty ( project.ProjectPlanFileUrl ) )
                {
                    pnlSummaryWrapInner.Style [ "background" ] = "none";
                    pnlProjectPlan.Visible = false;
                    projectDescriptionDiv.Attributes [ "style" ] = "width:99%";
                }
                else
                {
                    if ( project.ProjectStatus == ProjectStatus.Closed )
                    {
                        hlPlanFile.Enabled = false;
                    }
                    else
                    {
                        string absUrl = SPHelpers.GetPortalAbsoluteUrl ( SiteCollectionId, ServerRelativeUrl );
                        string docUrl = SPHelpers.GetHostHeader ( Request ) + project.ProjectPlanFileUrl;
                        string extension = project.ProjectPlanFileUrl.Substring ( project.ProjectPlanFileUrl.LastIndexOf ( "." ) + 1 );
                        string onClick = SPHelpers.GenerateDocumentOnClick ( docUrl, extension, PermissionBitsProjectPlanFile [ 2 ], absUrl );
                        hlPlanFile.Attributes.Add ( "onclick", onClick );
                    }

                    hlPlanFile.Text = project.ProjectPlanFileUrl.Substring ( project.ProjectPlanFileUrl.LastIndexOf ( "/" ) + 1 );
                    hlPlanFile.NavigateUrl = project.ProjectPlanFileUrl;
                }

                ProjectSummary.Project = project;

                // override page permissions and get object level permissions
                PermissionsModel.GetGlobalPermissionBitsForCurrentUser ( project, CurrentUser, SiteCollectionId, ServerRelativeUrl );
            }
        }

        protected override void OnPreRender ( EventArgs e )
        {
            base.OnPreRender ( e );
            pnlRecentActivity.Visible = ActivityHistory.Histories != null;
        }

        protected void btnUpdateStatus_Click ( object sender, EventArgs e )
        {
            project.EditType = EditType.Updated;
            project.ModifiedBy = CurrentUser;
            project.ModifiedDate = DateTime.Now;

            ProjectsModel.Save ( project, SiteCollectionId, ServerRelativeUrl );

            if ( project.IsValid ( ) )
            {
                Response.Redirect ( ServerRelativeUrl + Constants.ProjectFileUrls.ProjectSummary );
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/FEATURES/Kiiro.ProjectPages/Projects/Summary.aspx.cs $
 * 
 * 24    2/08/10 5:04p Camerons
 * 
 * 23    1/25/10 1:17p Camerons
 * 
 * 22    1/18/10 1:36p Camerons
 * 
 * 21    1/12/10 12:03p Camerons
 * 
 * 20    1/08/10 11:30a Camerons
 * 
 * 19    12/04/09 4:13p Shafia
 * 
 * 18    26/11/09 10:35a Camerons
 * 
 * 17    17/11/09 6:46p Camerons
 * 
 * 16    10/26/09 9:37a Adamw
 * 
 * 15    10/23/09 5:30p Adamw
 * Case 348
 * 
 * 14    30/09/09 4:59p Camerons
 * 
 * 13    30/09/09 4:08p Camerons
 * Update all links that point to UserPages, pass User.SPUserId as the
 * ItemId so that we can safely run GetOrCreateUser
 * 
 * 12    22/09/09 6:03p Camerons
 * Code changes to support creating user on demand - version 1.1.x.x
 * 
 * 11    21/09/09 3:30p Camerons
 * First batch of changes for 1.1.x.x - Security groups update
 * 
 * 10    2/09/09 4:59p Shafia
 * 
 * 9     2/09/09 4:36p Camerons
 * Repair for GenerateDocOnClick
 * 
 * 8     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/